<?php

namespace app\api\controller;

use app\admin\app\model\Material;
use app\admin\app\model\MaterialMerge;
use app\admin\app\model\Qiniu;
use app\admin\app\model\Video;
use support\exception\BusinessException;
use support\Request;

class ClippingController
{
    public function getMaterial()
    {
        $merge = MaterialMerge::where([
            'status' => 0
        ])->first();
        if (empty($merge)) {
            return json([
                'code' => 0,
                'msg' => '任务已完成',
            ]);
        }
        if (!$merge['is_random']) {
            $material = Material::whereIn('id', explode(',', $merge->material_ids))->where('admin_id', $merge->admin_id)->orderBy('group', 'desc')->get();
        } else {
            $material = [];
            $number = $merge->material_number;
            $not_id = [];
            for ($i = 0; $i < 8; $i++) {
                $m = Material::where('admin_id', $merge->admin_id)->where('category_id', $merge->category_id)->whereNotNull('path')->where('status', 1)->where('group', $i)->inRandomOrder()->first();
                if (!empty($m)) {
                    --$number;
                    $material[] = $m;
                    $not_id[] = $m->id;
                }
                if ($number === 0) {
                    break;
                }
            }
            $material = array_reverse($material);
            if ($number > 0) {
                $replenish = Material::where('admin_id', $merge->admin_id)->where('category_id', $merge->category_id)->whereNotIn('id', $not_id)->whereNotNull('path')->where('status', 1)->inRandomOrder()->limit($number)->get();
                if ($replenish->isNotEmpty()) {
                    $material = array_merge($replenish->toArray(), $material);
                }
            }


//            $main = Material::where('admin_id', $merge->admin_id)->whereNotNull('path')->where('status', 1)->where('group', 0)->first();
//            $number = $merge->material_number;
//            $map = [
//                ['admin_id', '=', $merge->admin_id],
//                ['path', '!=', ''],
//                ['status', '=', 1],
//            ];
//            if (!empty($main)) {
//                $main = $main->toArray();
//                --$number;
//                $map[] = ['id', '!=', $main['id']];
//            }
//            $material = Material::where($map)->inRandomOrder()->limit($number)->get();
//            if ($material->isNotEmpty()) {
//                $material = $material->toArray();
//            }
//            if (!empty($main)) {
//                array_push($material, $main);
//            }
        }
        $data = $merge->toArray();
        $data['material'] = $material;
        $merge->status = 1;
        $merge->log = json_encode($material);
        $merge->save();
        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $data
        ]);
    }

    public function saveMaterial(Request $request)
    {
        $file = current($request->file());
        $merge_id = $request->input('merge_id');
        if (!$file || !$file->isValid()) {
            return json(['code' => 0, 'msg' => '未找到文件']);
        }
        $merge = MaterialMerge::find($merge_id);
        if (empty($merge)) {
            return json(['code' => 0, 'msg' => '合并任务不存在']);
        }
        $qiniu = new Qiniu();
        list($data, $err) = $qiniu->upload($file);
        if ($err !== null) {
            return json(['code' => 0, 'msg' => '保存文件失败' . $err]);
        }
        $video = Video::create([
            'admin_id' => $merge->admin_id,
            'category_id' => $merge->category_id,
            'filename' => basename($data['key']),
            'path' => $data['key'],
            'merge_id' => $merge_id,
        ]);
        if ($video->id) {
            ++$merge->finish_number;
            if ($merge->finish_number === $merge->video_number) {
                $merge->status = 2;
            }
            $merge->save();
        }
        return json(['code' => 1, 'msg' => 'ok']);
    }

}
